package com.letv.core.db;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import com.letv.core.constant.DatabaseConstant;
import com.letv.core.constant.LetvConstant;

public class LetvContentProvider extends ContentProvider {

    public static  String AUTHORITY ;
    static {

		AUTHORITY = "com.letv.sdk.android.client.dao.LetvContentProvider.qingchun";
		if(AUTHORITY==null){
			AUTHORITY="com.letv.android.client.dao.LetvContentProvider.qingchun";
		}

//        try {
//            Properties properties = new Properties();
//            InputStream in = LetvContentProvider.class.getClassLoader().getResourceAsStream("letv.properties");
//            properties.load(in);
//            AUTHORITY = properties.getProperty("LetvContentProvider.authorities");
//            if(AUTHORITY==null){
//                AUTHORITY="com.letv.android.client.dao.LetvContentProvider";
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }
	public static final Uri URI_SEARCHTRACE = Uri.parse("content://" + AUTHORITY + "/"
			+ DatabaseConstant.SearchTrace.TABLE_NAME);
	public static final Uri URI_FAVORITETRACE = Uri.parse("content://" + AUTHORITY + "/"
			+ DatabaseConstant.FavoriteRecord.TABLE_NAME);
	public static final Uri URI_PLAYTRACE = Uri.parse("content://" + AUTHORITY + "/"
			+ DatabaseConstant.PlayRecord.TABLE_NAME);	
	public static final Uri URI_DOWNLOADTRACE = Uri.parse("content://" + AUTHORITY + "/"
			+ DatabaseConstant.DownloadTrace.TABLE_NAME);
	public static final Uri URI_FESTIVALIMAGE = Uri.parse("content://" + AUTHORITY + "/"
			+ DatabaseConstant.FestivalImageTrace.TABLE_NAME);
	public static final Uri URI_LOCALVIDEOTRACE = Uri.parse("content://" + AUTHORITY + "/"
			+ DatabaseConstant.LocalVideoTrace.TABLE_NAME);
	public static final Uri URI_LIVEBOOKTRACE = Uri.parse("content://" + AUTHORITY + "/"
			+ DatabaseConstant.LiveBookTrace.TABLE_NAME);
	public static final Uri URI_DIALOGMSGTRACE = Uri.parse("content://" + AUTHORITY + "/"
			+ DatabaseConstant.DialogMsgTrace.TABLE_NAME);
	public static final Uri URI_LOCALCACHETRACE = Uri.parse("content://" + AUTHORITY + "/"
			+ DatabaseConstant.LocalCacheTrace.TABLE_NAME);
	public static final Uri URI_CHANNELLISTTRACE=Uri.parse("content://"+AUTHORITY+"/"
			+ DatabaseConstant.ChannelListTrace.TABLE_NAME);
	public static final Uri URI_CHANNELHISLISTTRACE=Uri.parse("content://"+AUTHORITY+"/"
			+ DatabaseConstant.ChannelHisListTrace.TABLE_NAME);
	public static final Uri URI_TOPCHANNELSTRACE = Uri.parse("content://" + AUTHORITY + "/"
			+ DatabaseConstant.TopChannelsTrace.TABLE_NAME);
    /**
     * 口袋看球数据共享URI(v5.4.1)
     */
	public static final Uri URI_WORLDCUPTRACE = Uri.parse("content://" + AUTHORITY + "/"
			+ DatabaseConstant.WorldCupTrace.TABLE_NAME);

	/*
	 * 已观看的播放记录数据(v5.8)
	 */
	public static final Uri URI_PLAYTRACE_WATCHED = Uri.parse("content://" + AUTHORITY + "/"
			+ DatabaseConstant.PlayRecordWatched.TABLE_NAME);
	
	private static final int SEARCH_TRACE = 100;
	private static final int FAVORITE_TRACE = 101;
	private static final int PLAY_TRACE = 102;
	private static final int DOWNLOAD_TRACE = 103;
	private static final int FESTIVALIMAGE_TRACE = 104;
	private static final int LOCALVIDEO_TRACE = 105;
	private static final int LIVEBOOK_TRACE = 106;
	private static final int DIALOGMSG_TRACE = 107;
	private static final int LOCALCACHE_TRACE = 108;
	private static final int TOPCHANNELS_TRACE = 109;
	private static final int WORLDCUP_TRACE = 110;
	private static final int CHANNELLIST_TRACE=111;
	private static final int CHANNELHISLIST_TRACE=112;
	private static final int PLAY_TRACE_WATCHED=113;
	private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

	private SQLiteDataBase sqliteDataBase;

	static {
		URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.SearchTrace.TABLE_NAME, SEARCH_TRACE);
		URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.FavoriteRecord.TABLE_NAME, FAVORITE_TRACE);
		URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.PlayRecord.TABLE_NAME, PLAY_TRACE);
		URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.DownloadTrace.TABLE_NAME, DOWNLOAD_TRACE);
		URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.FestivalImageTrace.TABLE_NAME,FESTIVALIMAGE_TRACE);
		URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.LocalVideoTrace.TABLE_NAME, LOCALVIDEO_TRACE);
		URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.LiveBookTrace.TABLE_NAME, LIVEBOOK_TRACE);
		URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.DialogMsgTrace.TABLE_NAME, DIALOGMSG_TRACE);
		URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.LocalCacheTrace.TABLE_NAME, LOCALCACHE_TRACE);
		URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.TopChannelsTrace.TABLE_NAME, TOPCHANNELS_TRACE);
        /**
         * 口袋看球数据共享URI(v5.4.1)
         */
        URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.WorldCupTrace.TABLE_NAME, WORLDCUP_TRACE);
        URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.ChannelListTrace.TABLE_NAME, CHANNELLIST_TRACE);
        URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.ChannelHisListTrace.TABLE_NAME, CHANNELHISLIST_TRACE);
        /*
         * 已观看的播放记录数据(v5.8)
         */
        URI_MATCHER.addURI(AUTHORITY, DatabaseConstant.PlayRecordWatched.TABLE_NAME, PLAY_TRACE_WATCHED);
	}

	@Override
	public boolean onCreate() {
		sqliteDataBase = new SQLiteDataBase(getContext());
		return true;
	}

	@Override
	public String getType(Uri uri) {

		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {

		long rowId;
		Uri newUri = null;

		int match = URI_MATCHER.match(uri);

		SQLiteDatabase db = sqliteDataBase.getWritableDatabase();

		switch (match) {
		case SEARCH_TRACE:
			rowId = db.insert(DatabaseConstant.SearchTrace.TABLE_NAME,
					DatabaseConstant.SearchTrace.Field.NAME, values);
			if (rowId > 0) {
				newUri = ContentUris.withAppendedId(URI_SEARCHTRACE, rowId);
			}
			break;
		case FAVORITE_TRACE:
			rowId = db.insert(DatabaseConstant.FavoriteRecord.TABLE_NAME, null, values);
			if (rowId > 0) {
				newUri = ContentUris.withAppendedId(URI_FAVORITETRACE, rowId);
			}
			break;

		case PLAY_TRACE:
			rowId = db.insert(DatabaseConstant.PlayRecord.TABLE_NAME, null, values);
			if (rowId > 0) {
				newUri = ContentUris.withAppendedId(URI_PLAYTRACE, rowId);
			}
			break;
			
		case PLAY_TRACE_WATCHED:
			rowId = db.insert(DatabaseConstant.PlayRecordWatched.TABLE_NAME, null, values);
			if (rowId > 0) {
				newUri = ContentUris.withAppendedId(URI_PLAYTRACE_WATCHED, rowId);
			}
			break;

		case DOWNLOAD_TRACE:
			rowId = db.insert(DatabaseConstant.DownloadTrace.TABLE_NAME, null, values);
			if (rowId > 0) {
				newUri = ContentUris.withAppendedId(URI_DOWNLOADTRACE, rowId);
			}
			break;

		case FESTIVALIMAGE_TRACE:
			rowId = db.insert(DatabaseConstant.FestivalImageTrace.TABLE_NAME, null, values);
			if (rowId > 0) {
				newUri = ContentUris.withAppendedId(URI_FAVORITETRACE, rowId);
			}
			break;

		case LOCALVIDEO_TRACE:
			rowId = db.insert(DatabaseConstant.LocalVideoTrace.TABLE_NAME, null, values);
			if (rowId > 0) {
				newUri = ContentUris.withAppendedId(URI_LOCALVIDEOTRACE, rowId);
			}
			break;

		case LIVEBOOK_TRACE:
			rowId = db.insert(DatabaseConstant.LiveBookTrace.TABLE_NAME, null, values);
			if (rowId > 0) {
				newUri = ContentUris.withAppendedId(URI_LIVEBOOKTRACE, rowId);
			}
			break;
		case DIALOGMSG_TRACE:
			rowId = db.insert(DatabaseConstant.DialogMsgTrace.TABLE_NAME, null, values);
			if (rowId > 0) {
				newUri = ContentUris.withAppendedId(URI_DIALOGMSGTRACE, rowId);
			}
			break;
		case LOCALCACHE_TRACE:
			rowId = db.insert(DatabaseConstant.LocalCacheTrace.TABLE_NAME, null, values);
			if (rowId > 0) {
				newUri = ContentUris.withAppendedId(URI_LOCALCACHETRACE, rowId);
			}
			break;
		case TOPCHANNELS_TRACE:
			rowId = db.insert(DatabaseConstant.TopChannelsTrace.TABLE_NAME, null, values);
			if (rowId > 0) {
				newUri = ContentUris.withAppendedId(URI_LOCALCACHETRACE, rowId);
			}
			break;
        case WORLDCUP_TRACE:
            /**
             * 口袋看球数据共享URI insert(v5.4.1)
             */
            rowId = db.insert(DatabaseConstant.WorldCupTrace.TABLE_NAME, null, values);
            if (rowId > 0) {
                newUri = ContentUris.withAppendedId(URI_WORLDCUPTRACE, rowId);
            }
            break;
        case CHANNELLIST_TRACE:
        	rowId=db.insert(DatabaseConstant.ChannelListTrace.TABLE_NAME, null, values);
        	if(rowId>0){
        		newUri=ContentUris.withAppendedId(URI_CHANNELLISTTRACE,rowId);
        	}
        	break;
        case CHANNELHISLIST_TRACE:
        	rowId=db.insert(DatabaseConstant.ChannelHisListTrace.TABLE_NAME, null, values);
        	if(rowId>0){
        		newUri=ContentUris.withAppendedId(URI_CHANNELHISLISTTRACE,rowId);
        	}
        	break;
		default:
			throw new UnsupportedOperationException("Unknown or unsupported URL: " + uri.toString());

		}

		if (newUri != null) {
			getContext().getContentResolver().notifyChange(uri, null);
		}

		return newUri;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {

		int count;

		int match = URI_MATCHER.match(uri);
		SQLiteDatabase db = sqliteDataBase.getWritableDatabase();

		switch (match) {
		case SEARCH_TRACE:
			count = db.delete(DatabaseConstant.SearchTrace.TABLE_NAME, selection, selectionArgs);
			break;
		case FAVORITE_TRACE:
			count = db.delete(DatabaseConstant.FavoriteRecord.TABLE_NAME, selection, selectionArgs);
			break;
		case PLAY_TRACE:
			count = db.delete(DatabaseConstant.PlayRecord.TABLE_NAME, selection, selectionArgs);
			break;
		case PLAY_TRACE_WATCHED:
			count = db.delete(DatabaseConstant.PlayRecordWatched.TABLE_NAME, selection, selectionArgs);
			break;
		case DOWNLOAD_TRACE:
			count = db.delete(DatabaseConstant.DownloadTrace.TABLE_NAME, selection, selectionArgs);
			break;
		case FESTIVALIMAGE_TRACE:
			count = db.delete(DatabaseConstant.FestivalImageTrace.TABLE_NAME, selection, selectionArgs);
			break;
		case LOCALVIDEO_TRACE:
			count = db.delete(DatabaseConstant.LocalVideoTrace.TABLE_NAME, selection, selectionArgs);
			break;
		case LIVEBOOK_TRACE:
			count = db.delete(DatabaseConstant.LiveBookTrace.TABLE_NAME, selection, selectionArgs);
			break;
		case DIALOGMSG_TRACE:
			count = db.delete(DatabaseConstant.DialogMsgTrace.TABLE_NAME, selection, selectionArgs);
			break;
		case LOCALCACHE_TRACE:
			count = db.delete(DatabaseConstant.LocalCacheTrace.TABLE_NAME, selection, selectionArgs);
			break;
		case TOPCHANNELS_TRACE:
			count = db.delete(DatabaseConstant.TopChannelsTrace.TABLE_NAME, selection, selectionArgs);
			break;
		case WORLDCUP_TRACE:
            /**
             * 口袋看球数据共享URI delete(v5.4.1)
             */
			count = db.delete(DatabaseConstant.WorldCupTrace.TABLE_NAME, selection, selectionArgs);
			break;
		case CHANNELLIST_TRACE:
			count=db.delete(DatabaseConstant.ChannelListTrace.TABLE_NAME, selection, selectionArgs);
			break;
		case CHANNELHISLIST_TRACE:
			count=db.delete(DatabaseConstant.ChannelHisListTrace.TABLE_NAME, selection, selectionArgs);
			break;
		default:
			throw new UnsupportedOperationException("Unknown or unsupported URL: " + uri.toString());
		}

		this.getContext().getContentResolver().notifyChange(uri, null);

		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

		int count;
		int match = URI_MATCHER.match(uri);
		SQLiteDatabase db = sqliteDataBase.getWritableDatabase();

		switch (match) {
		case SEARCH_TRACE:
			count = db.update(DatabaseConstant.SearchTrace.TABLE_NAME, values, selection, selectionArgs);
			break;
		case PLAY_TRACE:
			count = db.update(DatabaseConstant.PlayRecord.TABLE_NAME, values, selection, selectionArgs);
			break;
		case PLAY_TRACE_WATCHED:
			count = db.update(DatabaseConstant.PlayRecordWatched.TABLE_NAME, values, selection, selectionArgs);
			break;
		case DOWNLOAD_TRACE:
			count = db.update(DatabaseConstant.DownloadTrace.TABLE_NAME, values, selection,
					selectionArgs);
			break;
		case FESTIVALIMAGE_TRACE:
			count = db.update(DatabaseConstant.FestivalImageTrace.TABLE_NAME, values, selection,
					selectionArgs);
			break;
		case LOCALVIDEO_TRACE:
			count = db.update(DatabaseConstant.LocalVideoTrace.TABLE_NAME, values, selection,
					selectionArgs);
			break;
		case LIVEBOOK_TRACE:
			count = db.update(DatabaseConstant.LiveBookTrace.TABLE_NAME, values, selection,
					selectionArgs);
			break;
		case DIALOGMSG_TRACE:
			count = db.update(DatabaseConstant.DialogMsgTrace.TABLE_NAME, values, selection,
					selectionArgs);
			break;
		case LOCALCACHE_TRACE:
			count = db.update(DatabaseConstant.LocalCacheTrace.TABLE_NAME, values, selection,
					selectionArgs);
			break;
		case TOPCHANNELS_TRACE:
			count = db.update(DatabaseConstant.TopChannelsTrace.TABLE_NAME, values, selection,
					selectionArgs);
		case WORLDCUP_TRACE:
            /**
             * 口袋看球数据共享URI update(v5.4.1)
             */
			count = db.update(DatabaseConstant.WorldCupTrace.TABLE_NAME, values, selection,
					selectionArgs);
			break;
		case CHANNELLIST_TRACE:
			count= db.update(DatabaseConstant.ChannelListTrace.TABLE_NAME, values, selection, 
					selectionArgs);
			break;
		case CHANNELHISLIST_TRACE:
			count= db.update(DatabaseConstant.ChannelHisListTrace.TABLE_NAME, values, selection, 
					selectionArgs);
			break;
		default:
			throw new UnsupportedOperationException("Unknown or unsupported URL: " + uri.toString());
		}

		this.getContext().getContentResolver().notifyChange(uri, null);

		return count;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
			String sortOrder) {

		Cursor cursor = null;
		int match = URI_MATCHER.match(uri);
		SQLiteDatabase db = sqliteDataBase.getWritableDatabase();
		switch (match) {
		case SEARCH_TRACE:
			cursor = db.query(DatabaseConstant.SearchTrace.TABLE_NAME, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case FAVORITE_TRACE:
			cursor = db.query(DatabaseConstant.FavoriteRecord.TABLE_NAME, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case PLAY_TRACE:
			cursor = db.query(DatabaseConstant.PlayRecord.TABLE_NAME, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case PLAY_TRACE_WATCHED:
			cursor = db.query(DatabaseConstant.PlayRecordWatched.TABLE_NAME, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case DOWNLOAD_TRACE:
			cursor = db.query(DatabaseConstant.DownloadTrace.TABLE_NAME, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case FESTIVALIMAGE_TRACE:
			cursor = db.query(DatabaseConstant.FestivalImageTrace.TABLE_NAME, null, selection,
					selectionArgs, null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case LOCALVIDEO_TRACE:
			cursor = db.query(DatabaseConstant.LocalVideoTrace.TABLE_NAME, null, selection,
					selectionArgs, null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case LIVEBOOK_TRACE:
			cursor = db.query(DatabaseConstant.LiveBookTrace.TABLE_NAME, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case DIALOGMSG_TRACE:
			cursor = db.query(DatabaseConstant.DialogMsgTrace.TABLE_NAME, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case LOCALCACHE_TRACE:
			cursor = db.query(DatabaseConstant.LocalCacheTrace.TABLE_NAME, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case TOPCHANNELS_TRACE:
			cursor = db.query(DatabaseConstant.TopChannelsTrace.TABLE_NAME, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case WORLDCUP_TRACE:
            /**
             * 口袋看球数据共享URI query(v5.4.1)
             */
			cursor = db.query(DatabaseConstant.WorldCupTrace.TABLE_NAME, null, selection, selectionArgs,
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case CHANNELLIST_TRACE:
			cursor=db.query(DatabaseConstant.ChannelListTrace.TABLE_NAME, null, selection, selectionArgs, 
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case CHANNELHISLIST_TRACE:
			cursor=db.query(DatabaseConstant.ChannelHisListTrace.TABLE_NAME, null, selection, selectionArgs, 
					null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		default:
			throw new UnsupportedOperationException("Unknown or unsupported URL: " + uri.toString());
		}
		return cursor;
	}
}
